<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品型号" prop="fDeviceModel">
        <el-input v-model="queryParams.fDeviceModel" placeholder="请输入产品型号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="添加日期">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:post:add']">新增客户预设</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column label="预设编号" align="center" prop="fId" />
      <el-table-column label="预设名称" align="center" prop="fClientPresetName" />
      <el-table-column label="客户内码" align="center" prop="fClientCode" />
      <el-table-column label="产品名称" align="center" prop="bcmProduct.fProductName" width="150" />
      <el-table-column label="产品型号" align="center" prop="bcmProduct.fDeviceModel" width="150" />
      <el-table-column label="IC芯片" align="center" prop="bcmProduct.fIcChip" />
      <el-table-column label="模组类型" align="center" prop="bcmProduct.wfModuleColor.fModuleName" width="150" />
      <el-table-column label="状态" align="center" prop="fForbidStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.fForbidStatus" />
        </template>
      </el-table-column>
      <el-table-column label="添加人" align="center" prop="fCreaterName" width="120" />
      <el-table-column label="添加日期" align="center" prop="fCreateDate" width="150" />
      <el-table-column label="最后修改人" align="center" prop="fModifierName" width="120" />
      <el-table-column label="最后修改时间" align="center" prop="fModifyDate" width="150" />
      <el-table-column fixed="right" label="操作" align="center" min-width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row, 1)"
            v-hasPermi="['system:post:edit']">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:post:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:post:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="65%">
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="客户预设值编码" prop="fClientPresetCode">
              <el-input v-model="form.fClientPresetCode" :disabled="true">
                {{ form.fClientPresetCode }}
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="预设值名称" prop="fClientPresetName">
              <el-input v-model="form.fClientPresetName" placeholder="请输入预设值名称" :disabled="isEdit == 1" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="产品" prop="fProductId">
              <div @click="openProduct" class="el-input-div">{{ product.fProductName }}</div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="产品型号" prop=" fDeviceModel">
              <el-input v-model="product.fDeviceModel" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="尺寸" prop="fProductSize">
              <el-input v-model="product.fProductSize" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="IC芯片" prop="fIcChip">
              <el-input v-model="product.fIcChip" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="模组类型" prop="fModuleType">
              <el-input v-model="product.wfModuleColor.fModuleName" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户" prop="fClientCode">
              <el-select v-loadmore="clientLoad" v-model="form.fClientCode" placeholder="请选择" :disabled="isEdit == 1">
                <el-option v-for="(item, index) in clientList" :key="index" :label="item.fClientCode"
                  :value="item.fClientCode"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="类型" prop="fClientPresetType">
              <template slot-scope="scope">
                <el-select clearable v-model="form.fClientPresetType" placeholder="请选择" style="width: 240px"
                  @change="queryParams.pageNum = 1, getList()" :disabled="isEdit == 1">
                  <el-option v-for="(item, index) in bcm_preset_type" :key="index" :label="item.label"
                    :value="item.value"></el-option>
                </el-select>
              </template>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="温度段(最小)" prop="fMinHeatStage">
              <el-input v-model="form.fMinHeatStage" placeholder="请输入温度段(最小)" :disabled="isEdit == 1" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="温度段(最大)" prop="fMaxHeatStage">
              <el-input v-model="form.fMaxHeatStage" placeholder="请输入温度段(最大)" :disabled="isEdit == 1" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="是否启用" prop="fForbidStatus">
          <el-radio-group v-model="form.fForbidStatus" :disabled="isEdit == 1">
            <el-radio v-for="dict in fForbidStatus" :key="dict.value" :label="dict.value">{{ dict.label
              }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="fRemark">
          <el-input v-model="form.fRemark" type="textarea" placeholder="请输入内容" :disabled="isEdit == 1" />
        </el-form-item><br>
        <el-form-item label="标准LAB值">
          <div>
            <el-table :data="form.bcmClientPresetDetailList">
              <el-table-column prop="color" align="center" label="颜色">
                <template slot-scope="scope">
                  <span>{{ colorDetail(scope.row.fColor) }}</span>
                </template>
              </el-table-column>
              <el-table-column label="最小 L 值" prop="fLMin" align="center">
                <template slot-scope="scope">
                  <el-input v-model.number="scope.row.fLMin" type="number" placeholder="请输入最小 L 值" clearable style="width: 100px"
                    :disabled="isEdit == 1" />
                </template>
              </el-table-column>
              <el-table-column label="最大 L 值" prop="fLMax" align="center">
                <template slot-scope="scope">
                  <el-input v-model.number="scope.row.fLMax" type="number" placeholder="请输入最大 L 值" clearable style="width: 100px"
                    :disabled="isEdit == 1" />
                </template>
              </el-table-column>
              <el-table-column label="最小 A 值" prop="fAMin" align="center">
                <template slot-scope="scope">
                  <el-input v-model.number="scope.row.fAMin" type="number" placeholder="请输入最小 A 值" clearable style="width: 100px"
                    :disabled="isEdit == 1" />
                </template>
              </el-table-column>
              <el-table-column label="最大 A 值" prop="fAMax" align="center">
                <template slot-scope="scope">
                  <el-input v-model.number="scope.row.fAMax" type="number" placeholder="请输入最大 A 值" clearable style="width: 100px"
                    :disabled="isEdit == 1" />
                </template>
              </el-table-column>
              <el-table-column label="最小 B 值" prop="fBMin" align="center">
                <template slot-scope="scope">
                  <el-input v-model.number="scope.row.fBMin" type="number" placeholder="请输入最小 B 值" clearable style="width: 100px"
                    :disabled="isEdit == 1" />
                </template>
              </el-table-column>
              <el-table-column label="最大 B 值" prop="fBMax" align="center">
                <template slot-scope="scope">
                  <el-input v-model.number="scope.row.fBMax" type="number" placeholder="请输入最大 B 值" clearable style="width: 100px"
                    :disabled="isEdit == 1" />
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-form-item><br>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="isEdit != 1">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <addPro ref="product" @sure="sureProduct"></addPro>
  </div>
</template>

<script>
import { listPreset, getPreset, delPreset, addPreset, updatePreset, wanPreset } from "@/api/basicData/preset";
import { listClient } from "@/api/basicData/customer"
import addPro from './components/addPro.vue';
export default {
  components: {
    addPro
  },
  name: "Preset",
  dicts: ['sys_normal_disable', 'wf_module_color', "bcm_preset_type"],
  data() {
    return {
      selectedModuleId: null,
      region: '',
      tableData: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      dateRange: "",

      dialogFormVisible: false,

      options: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fDeviceModel: undefined,
        fClientPresetName: undefined,
        fForbidStatus: undefined
      },
      // 表单参数
      form: {
        fForbidStatus: 0,
        fProductId: '',
        bcmClientPresetDetailList: []
      },
      product: {
        wfModuleColor: {
          fModuleName: ""
        }
      },
      // 表单校验
      rules: {
        fProductId: [
          { required: true, message: "产品不能为空", trigger: "blur" }
        ],
        fClientPresetName: [
          { required: true, message: "预设值名称不能为空", trigger: "blur" }
        ],
        fForbidStatus: [
          { required: true, message: "请选择是否启用不能选空", trigger: "change" }
        ],
        fMinHeatStage: [
          { required: true, message: "温度段(最小)不能为空", trigger: "blur" }
        ],
        fMaxHeatStage: [
          { required: true, message: "温度段(最大)不能为空", trigger: "blur" }
        ]
        //  为 fLabNum 添加校验规则
        //   fLabNum: [
        //   { required: true, message: '请输入标准 LAB 值', trigger: 'blur' }
        // ],
      },
      fForbidStatus: [
        { value: 0, label: '启用' },
        { value: 1, label: '停用' }
      ],
      bcm_preset_type: [
        { value: 0, label: '质检工单' },
        { value: 1, label: '波形调试工单' }
      ],
      fModuleWhere: { pageNum: 1, pageSize: 20 },
      clientWhere: { pageNum: 1, pageSize: 20 },
      clientList: [],
      fModuleList: [],
      isEdit: 0
    };
  },
  created() {
    this.getList();
  },
  methods: {
    colorDetail(id) {
      return this.dict.type.wf_module_color.find(item => item.value == id).label
    },
    clientLoad() {
      this.clientWhere.pageNum += 1
      this.getClient()
    },
    getClient() {
      listClient(this.clientWhere).then(res => {
        this.clientList = [...this.clientList, ...res.rows]
      })
    },
    fModuleLoad() {
      this.fModuleWhere.pageNum += 1
      this.getFactory()
    },
    getFactory() {
      wanPreset(this.fModuleWhere).then(res => {
        this.fModuleList = [...this.fModuleList, ...res.rows]
      })
    },
    sureProduct(e) {
      console.log(e)
      this.product = e
      this.form.fProductId = e.fId
      this.form.fProductName = e.fProductName
      this.form.fDeviceModel = e.fDeviceModel
      this.form.fProductSize = e.fProductSize
      this.form.fIcChip = e.fIcChip
      this.form.fModuleType = e.fModuleType
      this.form.fForbidStatus = e.fForbidStatus
      // this.form.fClientPresetType = e.fClientPresetType
      this.form.bcmClientPresetDetailList = e.wfModuleColor.wfModuleColorDetailList
    },
    openProduct() {
      this.$refs.product.open()
    },
    /** 查询岗位列表 */
    addDateRange(params, dateRange) {
      params.fCreateDateStart = dateRange[0];
      params.fCreateDateEnd = dateRange[1];
      return params;
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      listPreset(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fId: undefined,
        postCode: undefined,
        postName: undefined,
        postSort: 0,
        remark: undefined,
        fForbidStatus: 0,
      };

      this.clientWhere.pageNum = 1
      this.clientList = []
      this.fModuleWhere.pageNum = 1
      this.fModuleList = [],
        this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.isEdit = 0
      this.product = {
        wfModuleColor: {
          fModuleName: ""
        }
      }
      this.reset();
      this.getFactory();
      this.getClient();
      this.open = true;
      this.title = "新增客户预设";
    },
    /** 修改按钮操作 */
    handleUpdate(row, type) {
      this.isEdit = type
      this.reset();
      this.getFactory();
      this.getClient();
      const postId = row.fId || this.ids
      getPreset(postId).then(response => {
        this.form = response.data;
        this.product = response.data.bcmProduct
        this.form.fProductSize = response.data.bcmProduct.fProductSize
        this.form.bcmClientPresetDetailList = response.data.bcmClientPresetDetailList
        console.log("111111", response.data.bcmClientPresetDetailList);
        this.open = true;
        if (type === 1) {
          this.open = true;
          this.title = "查看客户预设";
        } else {
          this.open = true;
          this.title = "修改客户预设";
        }
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.fId != undefined) {
            updatePreset(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPreset(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = row.fId || this.ids;
      this.$modal.confirm('是否确认删除预设编号为"' + postIds + '"的数据项？').then(function () {
        return delPreset(postIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/post/export', {
        ...this.queryParams
      }, `post_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>

<style scoped lang="scss">
.el-input-div {
  width: 202px;
  height: 36px;
  box-sizing: border-box;
  padding: 0 15px;
  border: 1px solid #DCDFE6;
  border-radius: 4px;
  cursor: pointer;
  white-space: nowrap; // 强制一行显示
  overflow: hidden; // 超出隐藏
  text-overflow: ellipsis; // 省略号
}
</style>
